<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '350px',
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    isShow: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      chart: {
        setOption: (_) => {},
      },
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler: function (val) {
        this.setOptions(val)
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = {}
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData, true)
    },
    setOptions(chartData) {
      this.chart.clear()
      this.chart.setOption({
        xAxis: {
          data: chartData.timeData,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 10,
          right: 35,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        legend: {
          tooltip: {
            show: true,
          },
        },

        tooltip: {
          trigger: 'axis',
          // axisPointer: {
          //   type: 'cross',
          // },
          // padding: [5, 10],
          backgroundColor: 'rgba(255,255,255,0.8)', //设置背景图片 rgba格式
          color: 'black',
          borderWidth: '1', //边框宽度设置1
        },
        yAxis: {
          axisTick: {
            show: false,
          },
          axisLabel: {
            //这种做法就是在y轴的数据的值旁边拼接单位，貌似也挺方便的
            formatter: '{value} t',
          },
        },
        series: chartData.seriesData,
      })
    },
  },
}
</script>
